<?php
/**
 * Create by 11li
 * Date 2020/1/9
 * Time 19:19
 *
 */

namespace app\api\controller\v1;


use app\admin\model\User;
use app\common\controller\Api;
use app\common\service\CartService;
use app\common\service\OrderCommentService;
use app\common\service\OrderService;
use app\common\service\PaymentService;
use app\common\service\StatusService;

class Order extends Api
{
	
	/**
	 * 订单列表
	 */
	public function index($page = 1, $status = '') {
		$obj = new OrderService();
		
		$res = $obj->getList($this->auth->id, $page, $status);
		$this->success('查询成功', $res);
	}
	
	/**
	 * 确认订单
	 */
	public function confirm(){
		$data = $this->request->post();
		$userId = $this->auth->id;
		$obj = new OrderService();
		$res = $obj->calculation($userId, $data);
		if(!$res) {
			$this->error($obj->getError());
		}
		
		$res['address'] = $obj->getAddress($userId, $data['address_id'] ??'');
		
		$this->success('查询成功', $res);
	}
	
	
	/**
	 * 下单
	 */
	public function add(){
		$data = $this->request->post();
		$userId = $this->auth->id;
		$obj = new OrderService();
		$res = $obj->calculation($userId, $data);
		if(!$res) {
			$this->error($obj->getError());
		}
		
		
		$data['user_id'] = $userId;
		$data['recommend_user_id'] = $this->request->post('recommend_user_id', $this->auth->recommend_id, 'intval');
		if($res['order_type'] == StatusService::ORDER_TYPE_RESERVATION) {
			$orderIds = $obj->createReservationOrder($data, $res);
		} else {
			$orderIds = $obj->createOrder($data, $res);
		}
		if(!$orderIds) {
			$this->error($obj->getError());
		}
		
		//删除购物车
		if($data['from_type'] == 'cart' && !empty($data['ids'])) {
			$cartObj = new CartService();
			$ids = explode('_', $data['ids']);
			$cartRes = $cartObj->delByRows($ids, $userId);
		}
		
		$parmentObj = new PaymentService();
		$amount = $res['order_type'] == StatusService::ORDER_TYPE_NORMAL ? $res['amount']: $res['deposit'];
		$parmentId = $parmentObj->createRecord($orderIds, $userId, $amount);
		if(!$parmentId) {
			$this->error($parmentObj->getError());
		}
		
		$notify = url('v1/weixinNotify','', false, true);
		$user = User::get($userId);
		$parmentRes = $parmentObj->initPayment($parmentId, $amount, $notify, $user['smallapp_openid']);
		if(!$parmentRes) {
			$this->error($parmentObj->getError());
		}
		
		$parmentRes['cart_number'] = CartService::getCartNumber($userId);
		$parmentRes['amount'] = $res['amount'];
		$this->success('查询成功', $parmentRes);
	}
	
	
	/**
	 * 支付订单
	 */
	public function pay($id) {
		!$id && $this->error('参数错误');
		$obj = new OrderService();
		$res = $obj->payOrder($id, $this->auth->id);
		if($res) {
			$this->success('创建支付订单成功', $res);
		} else {
			$this->error($obj->getError());
		}
	}
	
	
	/**
	 * 提醒发货
	 */
	public function remindSendOrder($id) {
		!$id && $this->error('参数错误');
		
		$obj = new OrderService();
		$res = $obj->addRemindRecord($id, $this->auth->id);
		if($res) {
			$this->success('提醒成功');
		} else {
			$this->error($obj->getError());
		}
	}
	
	/**
	 * 签收订单
	 */
	public function signOrder($id) {
		!$id && $this->error('参数错误');
		
		$obj = new OrderService();
		$res = $obj->signOrder($id, $this->auth->id);
		if($res) {
			$this->success('签收成功');
		} else {
			$this->error($obj->getError());
		}
	}
	
	/**
	 * 删除订单
	 */
	public function deleteOrder($id) {
		!$id && $this->error('参数错误');
		
		$obj = new OrderService();
		$res = $obj->deleteOrder($id, $this->auth->id);
		if($res) {
			$this->success('删除成功');
		} else {
			$this->error($obj->getError());
		}
	}
	
	
	/**
	 * 订单详情
	 */
	public function orderDetail($id) {
		!$id && $this->error('参数错误');
		
		$obj = new OrderService();
		$res = $obj->getOrderDetail($id, $this->auth->id);
		if($res) {
			$this->success('查询成功', $res);
		} else {
			$this->error($obj->getError());
		}
	}
	
	/**
	 * 订单评价
	 */
	public function addOrderComment() {
		$data = $this->request->post();
		if(empty($data['order_id']) || empty($data['comment'])) {
			$this->error('参数错误');
		}
		
		$obj = new OrderCommentService();
		$res = $obj->addComment($data['order_id'], $data['comment'], $this->auth->id);
		if($res) {
			$this->success('评价成功');
		} else {
			$this->error($obj->getError());
		}
	}
}